package com.eboss2.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.adapter.ProfitDetailItemListAdapter;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.ProfitDetailItemInfo;
import com.eboss2.sbs.model.ProfitDetailItemInfoList;
import com.eboss2.sbs.myapplication.MyApplication;
import com.eboss2.sbs.weight.DialogUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.view.MyListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyProfitDetailItemListActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    @BindView(R.id.tv_mcht_name)
    TextView tvMchtName;
    @BindView(R.id.tv_count_time)
    TextView tvCountTime;
    @BindView(R.id.tv_get_person_num)
    TextView tvGetPersonNum;
    @BindView(R.id.tv_profit_amt)
    TextView tvProfitAmt;
    private PullToRefreshScrollView pullToRefreshScrollView;
    private MyListView listview;
    private List<ProfitDetailItemInfo> datas = new ArrayList<>();
    private ProfitDetailItemListAdapter adapter;
    private int page = 1;
    private int rows = Config.ROWS;

    String time;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_my_profit_detail_item_list);
        ButterKnife.bind(this);
        MyActivityManager.getInstance().addActivity(this);
        initView();
    }

    private void initView() {
        RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
        showView(R.id.titleback_btn_back, true);
        setTvText(R.id.tv_title, "分润明细列表");
        showView(R.id.next_sure, false);
        setTvText(R.id.next_sure, "确认收益");
        textView(R.id.next_sure).setTextSize(13);
        findViewById(R.id.next_sure).setOnClickListener(this);
        findViewById(R.id.titleback_btn_back).setOnClickListener(this);

        time = getIntent().getStringExtra("time");




        pullToRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pullToRefreshScrollView);
        pullToRefreshScrollView.setOnRefreshListener(new MyOnRefreshListener());
        pullToRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
        listview = (MyListView) findViewById(R.id.listview);
        listview.setOnItemClickListener(this);



        adapter = new ProfitDetailItemListAdapter(this, datas);
        listview.setAdapter(adapter);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        startActivity(new Intent(mContext, PointTransDetailActivity.class).putExtra("tradeId", datas.get(i).getTradeId()).putExtra("memberId", datas.get(i).getMemberId()));
    }


    class MyOnRefreshListener implements PullToRefreshBase.OnRefreshListener2<ScrollView> {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
            pullToRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
            String label = DateUtils.formatDateTime(mContext, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
            refreshData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
            page++;
            getdata();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        refreshData();
    }


    private void refreshData() {
        datas.clear();
        page = 1;
        getdata();
    }

    private void getdata() {
        showProgress("加载中...");

        String url = Config.SBS_PROFIT_DAY_LIST;
        RequestParams params = new RequestParams();
        params.put("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid() + "");
        params.put("statisDate", time);
        params.put("pageNo", page);
        params.put("pageSize", "15");

        HttpUtil.get(url, params, new HttpUtil.RequestListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void success(String response) {
                disShowProgress();
                try {

                    JSONObject result = new JSONObject(response);
                    if (!result.optBoolean("status")) {
                        showErrorMsg(result.getString("message"));
                        // 刷新完成
                        pullToRefreshScrollView.onRefreshComplete();
                        return;
                    }

                    Gson gson = new Gson();
                    // json数据转换成List
                    ProfitDetailItemInfoList txRecord = gson.fromJson(result.optString("data"), new TypeToken<ProfitDetailItemInfoList>() {
                    }.getType());

                    if (txRecord == null) {
                        pullToRefreshScrollView.onRefreshComplete();
                        return;
                    }

//                    tvMchtName.setText(txRecord.getProfitStatus());
					tvCountTime.setText(txRecord.getMonthDate()+"");
                    tvGetPersonNum.setText(txRecord.getNumber()+"");
                    tvProfitAmt.setText(StringUtils.formatIntMoney(txRecord.getProfitAmount()));

                    if (txRecord.getProfitDetailList().size() > 0) {
                        datas.addAll(txRecord.getProfitDetailList());
                        if (datas.size() > 0) {
                            linearLayout(R.id.id_no_data).setVisibility(View.GONE);
                            listview.setVisibility(View.VISIBLE);
                        } else {
                            linearLayout(R.id.id_no_data).setVisibility(View.VISIBLE);
                            listview.setVisibility(View.GONE);
                        }

                    } else {
                        if (datas.size() > 0) {

                            linearLayout(R.id.id_no_data).setVisibility(View.GONE);
                            listview.setVisibility(View.VISIBLE);
                            handler.postDelayed(new Runnable() {

                                @Override
                                public void run() {
                                    showErrorMsg("没有更多数据");
                                }
                            }, 1000);
                        } else {
                            linearLayout(R.id.id_no_data).setVisibility(View.VISIBLE);
                            listview.setVisibility(View.GONE);
                        }
                    }
                    // 刷新完成
                    pullToRefreshScrollView.onRefreshComplete();
                    adapter.notifyDataSetChanged();


                } catch (Exception e) {
                    e.getMessage();
                    pullToRefreshScrollView.onRefreshComplete();
                }
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
                pullToRefreshScrollView.onRefreshComplete();
            }
        });
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.titleback_btn_back:
                finish();
                break;
            case R.id.next_sure:
                String str = "2017年11月1日 00:00:00 -- 2017年11月30日 23:59:59\n" +
                        "共收益983.00元,确认后,金额将不可变化,账户余额将入账983.00元，请确认。";
                DialogUtils.showComfirmProfitDialog(mContext, str, null);
                break;
            default:
                break;
        }
    }


}
